package com.sto.stoapp.activity.sales;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SlectSignNameAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.manager.SalesSignNameManager;
import com.sto.stoapp.entity.SelectSignNameEntity;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

public class SalesSetSignNameActivity extends StoHomeActivity{

	private static final int REQUEST_CODE = 0xfd;
	// 添加
	private Button addBtn;
	// 列表
	private ListView signView;
	// 适配器
	private SlectSignNameAdapter signAdapter;
	
	// 数据
	private List<SelectSignNameEntity> commonSignList;
	
	// 返回
//	private TextView backView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sto_sales_set_sign_name);
		setTitleBottom(R.string.sales_sign_name_set, HomeBroadAction.SALES_MAN_BROAD);
		//setStoTitle(R.string.sales_sign_name_set);
		initWidget();
		initListener();
	}
	private void initWidget(){
		addBtn = (Button) findViewById(R.id.add);
		signView = (ListView) findViewById(R.id.sign_list);
//		backView = (TextView) findViewById(R.id.back);
		commonSignList = new ArrayList<SelectSignNameEntity>();
		new BufferThread(this).start();
	}
	
	private void initListener() {
		addBtn.setOnClickListener(new StoOnClickListener(this) {
			
			@Override
			public void mHonClick(View view) {
				Intent intent = new Intent(SalesSetSignNameActivity.this,
						SalesAddSignNameActivity.class);
				startActivityForResult(intent, REQUEST_CODE);
			}
		});
		
//		backView.setOnClickListener(new StoOnClickListener(this) {
//			
//			@Override
//			public void mHonClick(View view) {
//				finishEx();
//			}
//		});
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
			SelectSignNameEntity entity = data
					.getParcelableExtra(SalesAddSignNameActivity.SIGN_NAME);
			if (entity != null) {
				 commonSignList.add(entity);
				 signAdapter.notifyDataSetChanged();
				new BufferThread(this).start();
			}
		}
	}
	

	
	class BufferThread extends BaseThread {
		private Context context;

		public BufferThread(Context context) {
			super(context);
			this.context = context;
			showDialog();
		}

		@Override
		public void run() {
			super.run();
			commonSignList = SalesSignNameManager.getInstance(context).queryAll();
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					cancelDialog();
					signAdapter = new SlectSignNameAdapter(context, commonSignList,
							REQUEST_CODE);
					signView.setAdapter(signAdapter);
					if(signAdapter.getCount() > 0){
						signView.requestFocus();
					}
				}
			});
		}
	}
	

	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		commonSignList.clear();
		super.onDestroy();
	}
}
